package core

type Base struct {
	Timer []*Timer
}

func (b *Base) NewTimer() *Timer {
	t := &Timer{}
	b.Timer = append(b.Timer, t)
	return t
}

type Timer struct {
	// 定时器相关字段
	timerDuration  float64   // 定时时长（秒）
	timerRemaining float64   // 剩余时间（秒）
	timerTask      TimerTask // 定时任务
	isLooping      bool      // 是否循环
}
type TimerTask func()

// StartTimer 启动定时器，isLooping 控制是否循环
func (s *Timer) StartTimer(duration float64, task TimerTask, isLooping bool) {
	s.timerDuration = duration
	s.timerRemaining = duration
	s.timerTask = task
	s.isLooping = isLooping
}

// UpdateTimer 根据 dt 更新定时器
func (s *Timer) UpdateTimer(dt float64) {
	if s.timerRemaining > 0 {
		s.timerRemaining -= dt
		if s.timerRemaining <= 0 {
			if s.timerTask != nil {
				s.timerTask()
			}
			//fmt.Println("定时器触发")
			s.timerRemaining = s.timerDuration

			if s.isLooping {
				// 循环定时器，重置剩余时间
				s.timerRemaining = s.timerDuration
			} else {
				// 非循环定时器，重置状态
				s.timerRemaining = 0
				s.timerTask = nil
				s.isLooping = false
			}
		}
	}
}
